package com.bjb.vr.manager;

import com.bjb.vr.common.ao.ResContentImportAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.ResContent;
import com.bjb.vr.common.dto.ResDictionary;
import com.bjb.vr.common.service.ResContentService;
import com.bjb.vr.common.service.ResDictionaryService;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/8/18
 */
@SpringBootTest
public class DataImport {

	@Autowired
	ResDictionaryService resDictionaryService;

	@Autowired
	ResContentService resContentService;
	@Test
	public void importData() throws IOException {
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

		List<ResDictionary>  resDictionaryList = resDictionaryService.list();
		Map<String,String> dicMap = new HashMap<>();
		if(resDictionaryList!=null && !resDictionaryList.isEmpty()){
			for(ResDictionary dictionary:resDictionaryList){
				dicMap.put(dictionary.getDictName(),dictionary.getDictCode());
			}
		}


		File file = new File("g://XR平台课程表-xd.xlsx");
		FileInputStream fileInputStream = new FileInputStream(file);

		//1.1.根据Excel文件创建工作簿
		Workbook wb = new XSSFWorkbook(fileInputStream);
		//1.2.获取Sheet
		Sheet sheet = wb.getSheetAt(0);//参数：索引
		//1.3.获取Sheet中的每一行，和每一个单元格
		List<ResContentImportAo> list = new ArrayList<>();
		List<ResContent> resContentList = new ArrayList<>();
		for (int rowNum = 2; rowNum <= sheet.getLastRowNum(); rowNum++) {
			Row row = sheet.getRow(rowNum);//根据索引获取每一个行
			Object[] values = new Object[row.getLastCellNum()];
			for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
				Cell cell = row.getCell(cellNum);
				Object value = null;
				if(cell !=null) {
					value = getCellValue(cell);
				}
				values[cellNum] = value;
			}
			ResContentImportAo resContentImportAo = new ResContentImportAo(values,dicMap);
			resContentImportAo.setVersion("1.0");
			resContentImportAo.setContentSize(100);
			ResContent resContent = new ResContent();
			BeanUtils.copyProperties(resContentImportAo,resContent);
			resContentList.add(resContent);
		}

		System.out.println(resContentList);
		resContentService.saveBatch(resContentList);

	}


	private  Object getCellValue(Cell cell) {
		//1.获取到单元格的属性类型
		CellType cellType = cell.getCellType();
		//2.根据单元格数据类型获取数据
		Object value = null;
		switch (cellType) {
			case STRING:
				value = cell.getStringCellValue();
				break;
			case BOOLEAN:
				value = cell.getBooleanCellValue();
				break;
			case NUMERIC:
				if (DateUtil.isCellDateFormatted(cell)) {
					//日期格式
					value = cell.getDateCellValue();
				} else {
					//数字
					value = cell.getNumericCellValue();
				}
				break;
			default:
				break;
		}
		return value;
	}
}
